<?php

class RegisterController extends Controller
{
    private $_person;
    private $_user;

    private $_player;
    private $_currentTeam;

    public function actionManager()
    {
        $this->addManager();
    }

    public function actionStep1()
    {
        $person = $this->getPerson();
        $person->firstname = $_POST['firstName'];
        $person->lastname = $_POST['lastName'];
        $person->sex = $_POST['sex'];
        $person->birthdate = $_POST['birthday'];
        $person->citizenship_id = $_POST['citizenship'];
        $person->residence_id = $_POST['residence'];
        $person->mother_tongue = $_POST['motherTongue'];
        if (isset($_POST['otherLanguages'])) {
            $person->other_languages = implode(",", $_POST['otherLanguages']);
        }

        $user = $this->getUser();
        $user->password = $_POST['password'];

        if (isset($_FILES['file'])) {
            // store upload file (image)
            if (strcasecmp($_FILES["file"]["type"], "image/gif") == 0
                || strcasecmp($_FILES["file"]["type"], "image/jpeg") == 0
                || strcasecmp($_FILES["file"]["type"], "image/jpg") == 0
                || strcasecmp($_FILES["file"]["type"], "image/pjpeg") == 0
                || strcasecmp($_FILES["file"]["type"], "image/x-png") == 0
                || strcasecmp($_FILES["file"]["type"], "image/png") == 0
            ) {
                if ($_FILES["file"]["error"] > 0) {
                    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
                } else {
                    echo "valid" . $person->other_languages;
                    move_uploaded_file($_FILES["file"]["tmp_name"], "images/players/backup/" . $_FILES["file"]["name"]);
                    $person->upload = "images/players/backup/" . $_FILES["file"]["name"];
                }
            } else {
                echo 'no valid';
                echo $_FILES["file"]["type"];
                echo $_FILES["file"]["size"];
            }
        } else {
            echo "no file" . $person->other_languages;
        }
    }

    public function actionStep2()
    {
        $player = $this->getPlayer();
        $player->position = $_POST['position'];
        $player->hand = $_POST['hand'];
        $player->league = $_POST['league'];
        $player->height = $_POST['height'];
        $player->weight = $_POST['weight'];
        $player->status = $_POST['status'];
        $player->national = $_POST['national'];
        $player->achievements = $_POST['achievements'];

        $person = $this->getPerson();
        $person->available = $_POST['available'];
        $person->availability = $_POST['aMonth'] . '/' . $_POST['aYear'];
    }

    public function actionStep3()
    {
        $user = $this->getUser();
        $user->username = $_POST['email'];
        $user->save();

        $person = $this->getPerson();
        $person->user_id = $user->id;
        $person->email = $_POST['email'];
        $person->phone = $_POST['phone'];
        $person->video = $_POST['video'];

        $person->save();

        $player = $this->getPlayer();
        $player->person_id = $person->id;
        $player->save();
        if (isset($person->upload)) {
            $temp = explode(".", $person->upload);
            $extension = end($temp);

            copy($person->upload, "images/players/160.160/" . $person->id . '.' . $extension);
            rename($person->upload, "images/players/264.186/" . $person->id . '.' . $extension);
        }

        $teams = $this->getTeams();
        foreach ($teams as $item) {
            $item->player_id = $player->id;
            $item->start = $item->start . '/01';
            if (isset($item->end)) {
                $item->end = $item->end . '/01';
            }
            $item->save();
        }
        $detail = $this->getCurrentTeam();
        if (isset($detail->start)) {
            $detail->player_id = $player->id;
            $detail->start = $detail->start . '/01';
            $detail->save();
        }
        Yii::app()->session['_teams'] = null;
        Yii::app()->session['_current_team'] = null;
        Yii::app()->session['_person'] = null;
        Yii::app()->session['_player'] = null;
        Yii::app()->session['_user'] = null;

        $params = array('firstName' => $person->firstname);
        $this->sendMail('registration', 'Registration Confirmation', $person->email, $params);
    }

    public function actionTest()
    {
        $params = array('firstName' => 'dat tran');
        $this->sendMail('registration', 'Registration Confirmation', 'thanhdat3383@gmail.com', $params);
    }

    public function actionCheckEmail()
    {
        $person = Person::model()->findAll(array(
            'condition' => 'email=:email',
            'params' => array(':email' => $_GET['val'])
        ));
        if ($person != null) {
            echo 'true';
        }
    }

    public function actionTest1()
    {
        $_POST['sYear'] = 2013;
        $_POST['sMonth'] = 05;
        $_POST['eYear'] = 2014;
        $_POST['eMonth'] = 06;

        $start = new DateTime($_POST['sYear'] . '/' . $_POST['sMonth'] . '/15');
        $end = new DateTime($_POST['eYear'] . '/' . $_POST['eMonth'] . '/15');
        $interval = $start->diff($end);

        $result = (int)$interval->format('%R%a') > 0;
        if ($result) {
            $teams = $this->getTeams();
            if (count($teams) > 0) {
                $last = end($teams);
                $pEnd = new DateTime($last->end . '/15');

                $interval = $pEnd->diff($start);
                $result = (int)$interval->format('%R%a') > 0;
            }
        }
        echo $result ? "true" : "false";
    }

    public function actionCheckTeam()
    {
        $start = new DateTime($_POST['sYear'] . '/' . $_POST['sMonth'] . '/15');
        $end = new DateTime($_POST['eYear'] . '/' . $_POST['eMonth'] . '/15');
        $interval = $start->diff($end);

        $result = (int)$interval->format('%R%a') > 0;
        if ($result) {
            $teams = $this->getTeams();
            if (count($teams) > 0) {
                $last = end($teams);
                $pEnd = new DateTime($last->end . '/15');

                $interval = $pEnd->diff($start);
                $result = (int)$interval->format('%R%a') > 0;
            }
        }
        echo $result ? "true" : "false";
    }

    public function actionAddCurrentTeam()
    {
        $team = $this->getCurrentTeam();
        $team->start = $_POST['cYear'] . '/' . $_POST['cMonth'];
        $team->name = $_POST['team'];
        $team->country_id = $_POST['country'];
        $team->current = 1;
    }

    public function actionAddPreviousTeam()
    {
        $team = new Team();
        $team->start = $_POST['sYear'] . '/' . $_POST['sMonth'];
        $team->end = $_POST['eYear'] . '/' . $_POST['eMonth'];
        $team->country_id = $_POST['country'];

        $team->name = $_POST['team'];
        $team->current = 0;

        $teams = $this->getTeams();
        array_push($teams, $team);
        $this->setTeams($teams);

        echo count($teams) . '|' . count($this->getTeams());
    }

    public function getPlayer()
    {
        $this->_player = Yii::app()->session['_player'];
        if ($this->_player === null) {
            $this->_player = new Player;
            Yii::app()->session['_player'] = $this->_player;
        }
        return $this->_player;
    }

    public function getPerson()
    {
        $this->_person = Yii::app()->session['_person'];
        if ($this->_person === null) {
            $this->_person = new Person;
            Yii::app()->session['_person'] = $this->_person;
        }
        return $this->_person;
    }

    public function getUser()
    {
        $this->_user = Yii::app()->session['_user'];
        if ($this->_user === null) {
            $this->_user = new User();
            Yii::app()->session['_user'] = $this->_user;
        }
        return $this->_user;
    }

    public function getTeams()
    {
        $teams = Yii::app()->session['_teams'];
        if ($teams === null) {
            $teams = array();
            Yii::app()->session['_teams'] = $teams;
        }
        return $teams;
    }

    public function setTeams($teams)
    {
        Yii::app()->session['_teams'] = $teams;
    }

    public function getCurrentTeam()
    {
        $this->_currentTeam = Yii::app()->session['_current_team'];
        if ($this->_currentTeam === null) {
            $this->_currentTeam = new Team();
            Yii::app()->session['_current_team'] = $this->_currentTeam;
        }
        return $this->_currentTeam;
    }
}